package com.liuqi.knet.remoting.transport.dispatcher.handler;

import com.liuqi.knet.remoting.Channel;
import com.liuqi.knet.remoting.ChannelHandler;
import com.liuqi.knet.remoting.RemotingException;
import com.liuqi.knet.remoting.transport.AbstractChannelHandlerDelegate;
import com.liuqi.knet.remoting.transport.dispatcher.ChannelEventRunnable;
import java.util.concurrent.ExecutorService;

import static com.liuqi.knet.remoting.transport.dispatcher.ChannelEventRunnable.ChannelState.CAUGHT;
import static com.liuqi.knet.remoting.transport.dispatcher.ChannelEventRunnable.ChannelState.CONNECTED;
import static com.liuqi.knet.remoting.transport.dispatcher.ChannelEventRunnable.ChannelState.DISCONNECTED;
import static com.liuqi.knet.remoting.transport.dispatcher.ChannelEventRunnable.ChannelState.RECEIVED;

/**
 * 通道事件分发器
 *
 * @author liuqi
 * @date 2025/2/8 17:46
 **/
public class AllChannelHandler extends AbstractChannelHandlerDelegate {

    private final ExecutorService executor;

    public AllChannelHandler(ChannelHandler handler, ExecutorService executor) {
        super(handler);
        this.executor = executor;
    }

    @Override
    public void connected(Channel channel) throws RemotingException {
        executor.execute(new ChannelEventRunnable(channel, handler, CONNECTED));
    }

    @Override
    public void disconnected(Channel channel) throws RemotingException {
        executor.execute(new ChannelEventRunnable(channel, handler, DISCONNECTED));
    }

    @Override
    public void received(Channel channel, Object message) throws RemotingException {
        executor.execute(new ChannelEventRunnable(channel, handler, RECEIVED, message));
    }

    @Override
    public void caught(Channel channel, Throwable exception) throws RemotingException {
        executor.execute(new ChannelEventRunnable(channel, handler, CAUGHT, exception));
    }

}
